import {loadEnvApi} from "./load_env.js";
import { build } from "vite";
import vue from '@vitejs/plugin-vue';
import vueSetupExtend from 'vite-plugin-vue-setup-extend';
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';

import { resolve } from "path";
import { parseConfName, resoveModuleName } from "./load_env.js";
import RenamePlugin from "../plugins/rollup-plugin-rename.js";
import autoprefixer from 'autoprefixer';
import HtmlInject from "./../plugins/vite-plugin-html-injecet.js";


async function exec() {
    await loadEnvApi("publish");
    console.log("loadEnvApi  success");
    build({
        // cacheDir:false,
        configFile: false,
        root: process.cwd(),
        base: '/',
        publicDir:false,//不拷贝public
        plugins: [

            vue(),
            vueSetupExtend(),
            AutoImport({
                resolvers: [ElementPlusResolver({
                    exclude: /^ElAmap[A-Z]*/
                })],
            }),
            Components({
                resolvers: [ElementPlusResolver({
                    exclude: /^ElAmap[A-Z]*/
                })],
            }),
            RenamePlugin(resoveModuleName()),
            HtmlInject("publish"),
        ],
        resolve: {
            alias: {
                "~": resolve("./src"),
                "~components": resolve("./src/components"),
                "~api": resolve("./src/api/api.ignore.js"),
                "~tools":resolve("./src/public/tools"),
                "~assets": resolve("./src/assets"),
                "~public":resolve("./src/public"),
                "~Router":resolve("./src/public/tools/Router.js"),
                "~Rules":resolve("./src/public/rules/index.js"),
            },
        },
       
        define: {
            __ENV__: JSON.stringify(parseConfName()),
            __MODEL__:JSON.stringify("publish"),
        },
        build: {
            target:["es2015",'edge88', 'firefox78', 'chrome87', 'safari14'],
            sourcemap:false,
            assetsDir: resoveModuleName() + "_assets",

            // 代码压缩配置
            terserOptions: {
                // 生产环境移除console
                compress: {
                    drop_console: true,
                    drop_debugger: true,
                },
            },
        },
        css: {
            postcss: {
                plugins: [
                    autoprefixer()
                ]
            }
        },
    })
}

exec();